package com.bytedance.common.plugin.interfaces.pushmanager;

import android.content.Context;
import android.support.design.a;
import android.text.TextUtils;

import com.bytedance.common.plugin.interfaces.pushmanager.client.SsPushManager;
import com.bytedance.common.plugin.interfaces.pushmanager.setting.PushSetting;
import com.bytedance.common.plugin.interfaces.pushmanager.util.ToolUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.b.ThreadPlus;

import org.json.JSONObject;

import java.util.Map;

public class PushSettingManager {
    private static final String TAG = "PushSettingManager";
    private static PushSettingManager sPushSettingManager;

    public PushSettingManager() {

    }

    public static PushSettingManager getInstance() {
        PushSettingManager v0_1;
        Class v1 = PushSettingManager.class;
        synchronized (v1) {
            if (PushSettingManager.sPushSettingManager == null) {
                PushSettingManager.sPushSettingManager = new PushSettingManager();
            }

            v0_1 = PushSettingManager.sPushSettingManager;
        }
        return v0_1;
    }

    public boolean isPushNotifyEnable(Context arg2) {
        return PushSetting.getInstance().isPushNotifyEnable();
    }

    public void notifyAllowNetwork(Context context, boolean isAllowNetwork) {
        PushSetting.getInstance().setIsAllowNetwork(isAllowNetwork);
    }

    public void notifyAllowOffAlive(Context arg2, boolean arg3) {
        PushSetting.getInstance().setAllowOffAlive(arg3);
    }

    public void notifyAllowPushDaemonMonitor(Context arg2, boolean arg3) {
        PushSetting.getInstance().setAllowPushDaemonMonitor(arg3);
    }

    public void notifyAllowPushJobService(Context arg2, boolean arg3) {
        PushSetting.getInstance().setAllowPushJobService(arg3);
    }

    public void notifyAllowSettingsNotifyEnable(Context arg3, boolean arg4) {
        boolean v0 = PushSetting.getInstance().isAllowSettingsNotifyEnable();
        PushSetting.getInstance().setAllowSettingsNotifyEnable(arg4);
        if (!v0 && (arg4)) {
            SsPushManager.inst().registerAllThirdPush(arg3);
            SsPushManager.inst().registerSelfPush(arg3);
        }
    }

    public void notifyLoc(Context arg2, String arg3) {
        PushSetting.getInstance().setLoc(arg3);
    }

    public void notifyPushEnableChange(Context arg2, boolean arg3) {
        PushSetting.getInstance().setPushNotifyEnable(arg3);
        if (arg3) {
            SsPushManager.inst().registerAllThirdPush(arg2);
            SsPushManager.inst().registerSelfPush(arg2);
        } else {
            SsPushManager.inst().unRegisterAllThirdPush(arg2);
        }

        this.sendPushEnableToServer(arg2, arg3);
    }

    public void notifyShutPushOnStopService(Context arg2, boolean arg3) {
        PushSetting.getInstance().setIsShutPushOnStopService(arg3);
    }

    public void notifyUninstallQuestionUrl(Context arg2, String arg3) {
        PushSetting.getInstance().setUninstallQuestionUrl(arg3);
    }

    public void notifyWakeupBlackListPackages(Context arg2, String arg3) {
        PushSetting.getInstance().setWakeupBlackListPackages(arg3);
    }

    public void sendPushEnableToServer(final Context context, final boolean isEnable) {
        if (!a.isNetworkAvailable(context)) {
            PushSetting.getInstance().setLastSendNotifyEnableSucc(false);
        } else {
            new ThreadPlus() {
                public void run() {
                    int v1 = ToolUtils.areNotificationsEnabled(context);
                    Map v2 = SsPushManager.inst().getHttpCommonParams();
                    String v3 = "notice";
                    String v0 = isEnable ? "0" : "1";
                    v2.put(v3, v0);
                    v2.put("system_notify_status", String.valueOf(v1));
                    v0 = ToolUtils.addUrlParam(MessageConstants.SEND_NOTIFY_ENABLE_URL, v2);
                    try {
                        v0 = PushPluginDepend.inst().get(v0);
                        Logger.d("PushSettingManager", "sendPushEnableToServer response = " + v0);
                        if (!TextUtils.isEmpty(((CharSequence) v0)) && ("success".equals(new JSONObject(v0).optString("message")))) {
                            PushSetting.getInstance().setLastSendNotifyEnableSucc(true);
                            PushSetting.getInstance().setSystemPushEnable(v1);
                            return;
                        }

                        PushSetting.getInstance().setLastSendNotifyEnableSucc(false);
                    } catch (Exception v0_1) {
                        PushSetting.getInstance().setLastSendNotifyEnableSucc(false);
                        v0_1.printStackTrace();
                    }
                }
            }.start();
        }
    }

    public void setRequestSenderInterval(Context arg2, int arg3) {
        PushSetting.getInstance().setUpdateSenderIntervalTimeSecond(arg3);
    }
}

